<template>
  <el-dialog :visible="isShow" title="分配权限" @close="closeEvent">
    <!-- el-tree
           data
           props:{
               label:'label',
               children:'children'
           }
           show-checkbox:boolean,是否显示筛选框
           check-strictly:是否父子选择关联，默认是false
           default-checked-keys:[默认选择的项对应的值（值来自于node-key对应的字段）] 累加选中
           node-key:指定选中该项后的值
           this.$refs.el-tree的ref的值.setCheckedKeys([需要选中项的值]) 可以设置需要选中的项
           this.$refs.el-tree的ref的值.getCheckedKeys()  获取已选中项的结果
       -->
    <el-tree
      v-if="bol"
      ref="tree"
      :data="treeList"
      :props="{ label: 'name' }"
      show-checkbox
      check-strictly
      :default-checked-keys="defaultKeys"
      node-key="id"
    />
    <template #footer>
      <div style="text-align: center">
        <el-button @click="closeEvent">取消</el-button>
        <el-button type="primary" @click="submit">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script>
import { sysPermission } from '@/api/permission.js'
import { changeData } from '@/utils/index'
import { sysRoleById, sysRoleAssignPrem } from '@/api/setting.js'
export default {
  props: {
    isShow: {
      type: Boolean,
      default: false,
      required: true
    }
  },
  data() {
    return {
      treeList: [],
      defaultKeys: [],
      bol: true,
      id: ''
    }
  },
  created() {
    this.getData()
  },
  methods: {
    //   获取树形数据
    async getData() {
      const res = await sysPermission()
      this.treeList = changeData(res.data, '0')
      console.log('权限列表数据', res)
    },
    // 关闭弹框
    closeEvent() {
      this.$emit('update:isShow', false)
      //   还原组件成初始状态
      //   this.defaultKeys = []
      //   this.bol = false
      //   this.$nextTick(() => {
      //     this.bol = true
      //   })
      this.$refs.tree.setCheckedKeys([])
    },
    // 获取角色详情
    async getRoleInfo(row) {
      this.id = row.id
      const res = await sysRoleById(row.id)
      //   this.defaultKeys = res.data.permIds
      this.$refs.tree.setCheckedKeys(res.data.permIds)
      console.log('角色详情', res)
    },
    // 修改角色权限
    async submit() {
      await sysRoleAssignPrem(this.id, this.$refs.tree.getCheckedKeys())
      this.$message.success('分配权限成功')
      this.closeEvent()
    }
  }
}
</script>
<style></style>
